//
//  ViewController.swift
//  16.3 navigationBar
//
//  Created by 李煜 on 2024/6/24.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.prompt = "提示区域"
        
        setupNavigationBar()
        
    }
    
    
    func setupNavigationBar() {
        title = "主页"
        
//        UISearchController
//        UISearchBar
//        if #available(iOS 13.0, *) {
//            let appearance = UINavigationBarAppearance()
//            appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
//            navigationController?.navigationBar.standardAppearance = appearance
//            appearance.configureWithDefaultBackground()
//            appearance.backgroundColor = .red
//            navigationController?.navigationBar.standardAppearance = appearance
//        } else {
//            self.navigationController?.navigationBar.barTintColor = .gray
//            navigationController?.navigationBar.isTranslucent = false
//        }
        if #available(iOS 13.0, *) {
            let _appearance = UINavigationBarAppearance()
            // 重置背景和阴影颜色
            _appearance.configureWithDefaultBackground()
            // 去掉半透明效果
//            _appearance.backgroundEffect = nil
            //设置背景颜色
            _appearance.backgroundColor = .systemBlue
            //普通样式
            navigationController?.navigationBar.standardAppearance = _appearance;
            //滚动样式
            navigationController?.navigationBar.scrollEdgeAppearance = _appearance;
        }
    }
    
    
    func setupTranslucent() {
        let redView = UIView(frame: CGRect(x: 100, y: 0, width: 100, height: 100))
        redView.backgroundColor = .red
        self.view.addSubview(redView)
        //观察isTranslucent的false/true redView的坐标
        //        navigationController?.navigationBar.isTranslucent = false
        //
        //
        //        let btn = UIButton(type: .system)
        //        btn.frame = CGRect(x: 10, y: 100, width: 300, height: 40)
        //        btn.backgroundColor = .lightGray
        //        btn.setTitle("弹出新页面", for: .normal)
        //        btn.addTarget(self, action: #selector(click), for: .touchUpInside)
        //        view.addSubview(btn)
    }
    @objc func click() {
        
    }
    
    
    
    //设置渐变色背景
    //https://www.jianshu.com/p/4e17730b2c6e
    func setupNavigationBarBackgroundGradientColor() {
        if #available(iOS 13.0, *) {
            let navigationBarAppearance = UINavigationBarAppearance()
            navigationBarAppearance.configureWithDefaultBackground()
            let gradientLayer = CAGradientLayer()
            gradientLayer.colors = [UIColor.blue.cgColor,UIColor.green.cgColor]
            gradientLayer.startPoint = CGPointMake(0.0, 0.0)
            gradientLayer.endPoint = CGPointMake(1.0, 0.0)
            
            navigationBarAppearance.backgroundEffect = UIBlurEffect(style: .light)
            
            // image render
            let render = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1))
            let backgroundImage = render.image { ctx in
                gradientLayer.frame = .init(x: 0, y: 0, width: ctx.format.bounds.width, height: ctx.format.bounds.height)
                gradientLayer.render(in: ctx.cgContext)
            }
            navigationBarAppearance.backgroundImage = backgroundImage
            
            navigationController?.navigationBar.standardAppearance = navigationBarAppearance
            navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
        } else {
            
        }
    }

}

